package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcPointCardAccountMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.PointCardAccountConstant;
import com.csun.cmny.provider.model.domain.OmcPointCardAccount;
import com.csun.cmny.provider.model.dto.PointCardAccountDto;
import com.csun.cmny.provider.model.dto.PointCardAccountQueryDto;
import com.csun.cmny.provider.model.vo.PointCardAccountVO;
import com.csun.cmny.provider.service.OmcPointCardAccountService;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
@Transactional
public class OmcPointCardAccountServiceImpl extends BaseService<OmcPointCardAccount> implements OmcPointCardAccountService {

    @Resource
    private OmcPointCardAccountMapper omcPointCardAccountMapper;

    @Override
    public List<PointCardAccountVO> listPointCardAccount(PointCardAccountQueryDto dto) {
        return omcPointCardAccountMapper.listPointCardAccount(dto);
    }

    @Override
    public int addPointCardAccount(PointCardAccountDto dto) {

        OmcPointCardAccount omcPointCardAccount = omcPointCardAccountMapper.selectByPrimaryKey(dto.getDeviceId());
        if(PublicUtil.isNotEmpty(omcPointCardAccount)){
            throw new OmcBizException(ErrorCodeEnum.OMC10080001);
        }

        omcPointCardAccount = new OmcPointCardAccount();
        omcPointCardAccount.setUpdateInfo(dto.getLoginAuthDto());
        omcPointCardAccount.setDeviceId(dto.getDeviceId());

        return omcPointCardAccountMapper.insertSelective(omcPointCardAccount);
    }

    @Override
    public int freeze(Long deviceId) {

        OmcPointCardAccount omcPointCardAccount = omcPointCardAccountMapper.selectByPrimaryKey(deviceId);

        if (omcPointCardAccount != null && omcPointCardAccount.getStatus().equals(PointCardAccountConstant.INUSE_CARD_STATUS)) {
            omcPointCardAccount.setStatus(PointCardAccountConstant.FREEZE_HUMAN_TYPE);
        } else {
            throw new DmcBizException(ErrorCodeEnum.DMC100617004);
        }
        return omcPointCardAccountMapper.updateByPrimaryKey(omcPointCardAccount);
    }

    @Override
    public int unfreeze(Long deviceId) {

        OmcPointCardAccount bind = omcPointCardAccountMapper.selectByPrimaryKey(deviceId);

        if (bind != null && bind.getStatus().equals(PointCardAccountConstant.FREEZE_HUMAN_TYPE)) {
            bind.setStatus(PointCardAccountConstant.INUSE_CARD_STATUS);
        } else {
            throw new DmcBizException(ErrorCodeEnum.DMC100617005);
        }
        return omcPointCardAccountMapper.updateByPrimaryKey(bind);
    }

    @Override
    public int refund(Long deviceId) {

        OmcPointCardAccount bind = omcPointCardAccountMapper.selectByPrimaryKey(deviceId);

        if (bind != null && bind.getStatus().equals(PointCardAccountConstant.INUSE_CARD_STATUS)) {
            bind.setStatus(PointCardAccountConstant.RETURN_HUMAN_TYPE);
            //TODO 退费

        } else {
            throw new DmcBizException(ErrorCodeEnum.DMC100617005);
        }
        return omcPointCardAccountMapper.updateByPrimaryKey(bind);
    }
}
